package com.admin.service;

import com.admin.dao.StockDao;
import com.admin.entity.Stock;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.utils.admin.ObjectUtils;
import com.admin.sys.base.module.extend.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

//库存信息接口类
@Service
public class StockService extends BaseService {

    @Autowired
    private StockDao stockDao;

    //【库存信息】设置查询条件
    private LambdaQueryWrapper<Stock> getStockQueryCondition(Stock stock) {
        LambdaQueryWrapper<Stock> lambdaQuery = this.getBaseQueryCondition(stock);
        //根据创建时间排序
        lambdaQuery.orderByDesc(Stock::getCreateDate);
        if (ObjectUtils.isNotNull(stock)) {
            //【主键id】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(stock.getId()), Stock::getId, stock.getId());
            //【物品名称】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(stock.getName()), Stock::getName, stock.getName());
            //【库位名称】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(stock.getAddr()), Stock::getAddr, stock.getAddr());
            //【供应商名称】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(stock.getSupplierId()), Stock::getSupplierId, stock.getSupplierId());
            //【物品类别】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(stock.getCategoryId()), Stock::getCategoryId, stock.getCategoryId());
            //【预警值】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(stock.getWarnNum()), Stock::getWarnNum, stock.getWarnNum());
        }
        return lambdaQuery;
    }

    //【库存信息】分页查询
    public IPage<Stock> getPage(Page<Stock> page, Stock stock) {
        LambdaQueryWrapper<Stock> lambdaQuery = getStockQueryCondition(stock);
        return stockDao.selectPage(page, lambdaQuery);
    }
    
    //【库存信息】查询列表
    public List<Stock> getList(Stock stock) {
        LambdaQueryWrapper<Stock> lambdaQuery = getStockQueryCondition(stock);
        return stockDao.selectList(lambdaQuery);
    }
    
    //【库存信息】根据id查询
    public Stock get(String id) {
        return stockDao.selectById(id);
    }

    //【库存信息】根据对象查询
    public Stock get(Stock stock) {
    LambdaQueryWrapper<Stock> lambdaQuery = getStockQueryCondition(stock);
        return stockDao.selectOne(lambdaQuery);
    }
    
    //【库存信息】新增
    public int insert(Stock stock) {
        this.preInsert(stock);
        return stockDao.insert(stock);
    }
    
    //【库存信息】修改
    public int update(Stock stock) {
        this.preUpdate(stock);
        return stockDao.updateById(stock);
    }
    
    //【库存信息】删除
    public int delete(String id) {
        return stockDao.deleteById(id);
    }

    //【库存信息】批量删除
    public int delAll(String[] ids) {
        return stockDao.deleteBatchIds(Arrays.asList(ids));
    }

}